<?php

namespace app\admin\controller\businessmanage;
use think\exception\DbException;
use think\exception\PDOException;
use Exception;
use think\Db;

use app\common\controller\Backend;

/**
 * 商机
 *
 * @icon fa fa-circle-o
 */
class Business extends Backend
{

    /**
     * Business模型对象
     * @var \app\admin\model\businessmanage\Business
     */
    protected $model = null;
    protected $searchFields = 'b_name,c_name,mobile';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\businessmanage\Business;
        $this->view->assign("pushswitchList", $this->model->getPushswitchList());
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        $action = $this->request->get('action');
        if($action =='getType'){
            return getTypes();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with(['btype'])
            ->field('btype.name as bname,btype.id as bid')
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as $k => $v) {
            $v['bid'] = $v['bname'];
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    } 
    /**
     * 撤销领取
     *
     * @param $ids
     * @return string
     * @throws DbException
     * @throws \think\Exception
     */
    public function revoke($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        $params = $this->request->param('ids');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $result = false;
        Db::startTrans();
        try {
            //会员点数变动
            $business = Db::name('business')->find($params);
            $user = Db::name('admin')->find($business['owner_id']);
            $user_point = $user['point'];
            $udata['id'] = $business['owner_id'];
            $udata['point'] = $user_point + $business['point'];
            $result = Db::name('admin')->update($udata);
            if(!$result){
                $this->error(__('No rows were updated'));
            }
            //点数变动日志
            $log_data['user_id'] = $business['owner_id'];
            $log_data['score'] = $udata['point'];
            $log_data['before'] = $user_point;
            $log_data['after'] = $udata['point'];
            $log_data['createtime'] =time();
            $log_data['memo'] = '管理员撤销用户:'.$user['username'].' 的项目'.$business['b_name'].' 退还点数';
            $result = Db::name('user_point_log')->insert($log_data);
            if(!$result){
                $this->error(__('No rows were updated'));
            }
            //项目更改为未领取
            $data['owner_id'] = null;
            $data['status'] = 0;
            $result = Db::name('business')->where('id',$params)->update($data);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
